//
//  ReentyBlock.mm
//  sad
//
//  Created by Christopher Kemsley on 3/21/12.
//  Copyright (c) 2012 iAppolopolis. All rights reserved.
//

#include "ReentyBlock.hpp"
#include "Exception.hpp"


ReentyBlock :: ReentyBlock	( char const * identifier )
:	name	( identifier )
,	trace	(  )
	{
	if ( currentBlock() != 0 )
		{
		std::cout << "Block @ \""<<name<<"\" re-entered. Other entry: " << currentBlock()->trace ;
		ThrowException ( "Reentry block" , "Block @ \""<<name<<"\" re-entered. Other entry: " << currentBlock()->trace , __FILE__,__LINE__ ) ;
		}
	currentBlock() = this ;
	}
ReentyBlock :: ~ReentyBlock ()
	{
	currentBlock() = 0 ;
	}

std::map<std::string,ReentyBlock*>&	ReentyBlock :: _map()
	{
	static std::map<std::string,ReentyBlock*> s ;
	return s ;
	}


ReentyBlock*&	ReentyBlock :: currentBlock ()
	{
	std::map<std::string,ReentyBlock*>::iterator it = _map().find(name) ;
	if ( it == _map().end() )
		{
		_map()[name] = 0 ;
		return _map()[name] ;
		}
	return it->second ;
	}
